package com.demomaster.quickcompat.widget.button;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.appcompat.widget.AppCompatTextView;

import com.demomaster.quickcompat.widget.base.QuickView;
import com.demomaster.quickcompat.widget.base.QuickViewImpl;
import com.demomaster.quickcompat.widget.base.QuickViewUtil;

/**
 * @author squirrel桓
 * @date 2019/1/7.
 * description：
 */
public class QuickTextView extends AppCompatTextView implements QuickView {
    public QuickTextView(Context context) {
        super(context);
        init(context, null, 0);
    }

    public QuickTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public QuickTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    QuickViewImpl quickViewImpl;

    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        quickViewImpl = new QuickViewImpl(this, attrs, defStyleAttr);
    }

    @Override
    public void setClickable(boolean clickable) {
        super.setClickable(clickable);
        QuickViewUtil.setClickable(this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        QuickViewUtil.draw(canvas, quickViewImpl);
        super.onDraw(canvas);

        if (hollowOut) {
            //禁用硬件加速
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            RectF rectf = new RectF(0, 0, getWidth(), getHeight());
            Paint paint = new Paint();
            paint.setColor(hollowColor);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));
            canvas.drawRoundRect(rectf, hollowRadius, hollowRadius, paint);
            paint.setXfermode(null);
        }
        QuickViewUtil.drawBadge(this, canvas, quickViewImpl);
    }

    boolean hollowOut;
    int hollowColor = Color.WHITE;
    float hollowRadius = 5;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if (quickViewImpl.attribute.warpTextLine) {
            int maxLines = getMaxLines(); // 设置最大行数为 2
            int lineHeight = getLineHeight();
            int totalLines = getLayout().getLineCount();
//TODO
            //根据提供的测量值提取模式
            //int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            int heightMode = MeasureSpec.getMode(heightMeasureSpec);
//
//                if (heightMode == MeasureSpec.EXACTLY) {
//                    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
//                    widthMeasureSpec = MeasureSpec.makeMeasureSpec((int) (heightSize*quickViewImpl.attribute.aspectRatio), heightMode);
//                }

            int height = MeasureSpec.getSize(heightMeasureSpec);
            int lines = height / lineHeight;
            setLines(lines);
//            if (totalLines > maxLines) {
//                //int height = lineHeight * maxLines;
//                //setMeasuredDimension(getMeasuredWidth(), height);
//            }
        }
    }
}
